package com.fmdj.customer.controller;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.customer.controller.form.CreateOrderForm;
import com.fmdj.customer.controller.form.LoginForm;
import com.fmdj.customer.controller.form.RegisterNewCustomerForm;
import com.fmdj.customer.service.CustomerService;
import com.fmdj.customer.service.OrderService;
import com.fmdj.customer.utils.ReturnMessage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;

@RestController
@RequestMapping("/customer")
@Tag(name = "CustomerController", description = "客户Web接口")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private OrderService orderService;
    @PostMapping("/registerNewCustomer")
    @Operation(summary = "注册用户")
    public CommonResult registerNewCustomer(@Valid @RequestBody RegisterNewCustomerForm form) {
        String customerId = customerService.registerNewCustomer(form);
        StpUtil.login(customerId);
        String tokenValue = StpUtil.getTokenInfo().getTokenValue();
        return CommonResult.ok().put(CommonResult.RETURN_TOKEN, tokenValue);
    }

    @PostMapping("/login")
    @Operation(summary = "登录")
    public CommonResult login(@Valid @RequestBody LoginForm form) {
        Long customerId = customerService.login(form);
        if (customerId != null) {  /*注册成功一定有乘客id所以不需要判空 但是用户登录可能查询到空 需判空*/
            StpUtil.login(customerId);
            String tokenValue = StpUtil.getTokenInfo().getTokenValue();
            return CommonResult.ok().put(CommonResult.RETURN_TOKEN, tokenValue);
        }
        return CommonResult.ok();
    }
}
